import pymysql as pymysql

from Ini_utils import UtilIni
from utils.logging_util import Log


class ConnDb:
    @staticmethod
    def d_connect_DB(database, sql):
        """

        :param database: 数据库名称
        :param sql: 查询的sql语句
        :return: 查询结果
        """
        try:
            connect = pymysql.connect(
                host=UtilIni().getvalue("DEV", "host"),
                user=UtilIni().getvalue("DEV", "user"),
                password=UtilIni().getvalue("DEV", "password"),
                database=database)
            cursor = connect.cursor()
            cursor.execute(sql)
            res = cursor.fetchone()
            cursor.close()
            connect.close()
            Log().info("连接成功...")
            return res
        except Exception as e:
            Log().error("数据库配置错误，连接失败...")
            Log().error(e)
            return False

    # 连接数据库返回connect
    @staticmethod
    def f_connect_DB(database, sql):
        """

        :param database: 数据库名称
        :param sql: 查询的sql语句
        :return: 查询结果
        """
        try:
            connect = pymysql.connect(
                host=UtilIni().getvalue("FAT", "host"),
                user=UtilIni().getvalue("FAT", "user"),
                password=UtilIni().getvalue("FAT", "password"),
                database=database)
            cursor = connect.cursor()
            cursor.execute(sql)
            res = cursor.fetchone()
            cursor.close()
            connect.close()
            Log().info("连接成功...")
            return res
        except Exception as e:
            Log().error("数据库配置错误，连接失败...")
            Log().error(e)
            return False

    @staticmethod
    def u_connect_DB(database, sql):
        """

        :param database: 数据库名称
        :param sql: 查询的sql语句
        :return: 查询结果
        """
        try:
            connect = pymysql.connect(
                host=UtilIni().getvalue("UAT", "host"),
                user=UtilIni().getvalue("UAT", "user"),
                password=UtilIni().getvalue("UAT", "password"),
                database=database)
            cursor = connect.cursor()
            cursor.execute(sql)
            res = cursor.fetchone()
            cursor.close()
            connect.close()
            Log().info("数据库配置正确，连接成功...")
            return res
        except Exception as e:
            Log().error("数据库配置错误，连接失败...")
            Log().error(e)
            return False
